#ParallelPlatform Project - A web platform to distribute and share knowledge
#Copyright (C) 2011  Daniel Guerrero Martinez & Sergio Rodriguez Lumley
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
from django import forms
from registration.forms import RegistrationFormUniqueEmail, RegistrationFormTermsOfService
from django.contrib.auth.models import User
from datetime import datetime
from django.forms import EmailField


from django.contrib.auth.forms import AuthenticationForm

class myAuthenticationForm(AuthenticationForm):
    
    username = EmailField()


class RegistrationForm(RegistrationFormUniqueEmail, RegistrationFormTermsOfService ):
    """
    A registration form that only requires the user to enter their e-mail 
    address and password. The username is automatically generated
    This class requires django-registration to extend the 
    RegistrationFormUniqueEmail
    """
    username = forms.CharField(widget=forms.HiddenInput, required=False)

    def clean_username(self):
        "This function is required to overwrite an inherited username clean"
        return self.cleaned_data['username']

    def clean(self):
        if not self.errors:
            self.cleaned_data['username'] = self.cleaned_data['email'].split('@')[0]
        super(RegistrationForm, self).clean()
        return self.cleaned_data


class UserChange(forms.ModelForm):
    
    username = forms.CharField(required=True)

    def clean_username(self):
        return self.cleaned_data['username']

    class Meta:
        model = User
        fields = ('username',)


class RemoveUser(forms.ModelForm):
    
    username = forms.CharField(widget=forms.HiddenInput, required=False)
    first_name = forms.CharField(widget=forms.HiddenInput, required=False)
    last_name = forms.CharField(widget=forms.HiddenInput, required=False)
    email = forms.EmailField(widget=forms.HiddenInput, required=False)
    password = forms.CharField(widget=forms.PasswordInput)
    is_staff = forms.BooleanField(widget=forms.HiddenInput, required=False)
    is_active = forms.BooleanField(widget=forms.HiddenInput, required=False)
    is_superuser = forms.BooleanField(widget=forms.HiddenInput, required=False)
    visibleContributor = forms.BooleanField(widget=forms.HiddenInput, required=False)
    
    def clean(self):
        if not self.errors:
            if self.instance.check_password(self.cleaned_data['password']):
                self.cleaned_data['username'] = str(datetime.now())
                self.cleaned_data['first_name'] = ""
                self.cleaned_data['last_name'] = ""
                self.cleaned_data['email'] = ""
                self.cleaned_data['is_staff'] = False
                self.cleaned_data['is_active'] = True
                self.cleaned_data['is_superuser'] = False
                self.cleaned_data['visibleContributor'] = False
            else:
                raise forms.ValidationError(_("Password is not valid"))
            
        super(RemoveUser, self).clean()
        return self.cleaned_data
    
    class Meta:
        model = User
        fields = ('username','first_name','last_name','email','is_staff','is_active','is_superuser','visibleContributor')


